package com.copa.l234;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author copa
 * @createDate 2022-05-31 09:10
 * @function 双指针法
 */
public class Test2 {

    public boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            // 先把元素放到list中
            list.add(head.val);
            head = head.next;
        }
        for (int i = 0; i < list.size() / 2; i++) {
            // 这里其实就是用i和list.size() - i - 1所指向的val值进行一一比对，相当于就是双指针
            if (!Objects.equals(list.get(i), list.get(list.size() - i - 1))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(2);
        head.next.next.next = new ListNode(1);
//        head.next.next.next.next = new ListNode(1);
        System.out.println(new Test2().isPalindrome(head));
    }
}
